package com.been.common.dao.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.been.common.dao.PublicDAO;

/**
 * PubDAO实现类，继承自{@link HibernateDaoSupport}，
 * 实现了PublicDAO，对PublicDAO里的方法的实现均基于HibernateDaoSupport
 * 
 * @author been 2011-09-28
 */
public class PublicDAOImpl extends HibernateDaoSupport implements PublicDAO {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#save(java.lang.Object)
	 */
	public void save(Object object) {
		this.getHibernateTemplate().save(object);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#delete(java.lang.Object)
	 */
	public void delete(Object object) {
		this.getHibernateTemplate().delete(object);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#update(java.lang.Object)
	 */
	public void update(Object object) {
		this.getHibernateTemplate().update(object);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#find(java.lang.Class,
	 * java.io.Serializable)
	 */
	@SuppressWarnings("unchecked")
	public <M> M find(Class<M> entityClass, Serializable id) {
		return (M) this.getHibernateTemplate().get(entityClass, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#find(java.lang.Class,
	 * java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public <M> List<M> find(Class<M> entityClass, String hql) {
		return this.getHibernateTemplate().find(hql);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#createQuery(java.lang.String)
	 */
	public Query createQuery(String hql) {
		return getCurSession().createQuery(hql);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#createSQLQuery(java.lang.String)
	 */
	public Query createSQLQuery(String sql) {
		return getCurSession().createSQLQuery(sql);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#getCurSession()
	 */
	public Session getCurSession() {
		return this.getSession();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#deleteAll(java.util.List)
	 */
	public <M> void deleteAll(List<M> objList) {
		this.getHibernateTemplate().deleteAll(objList);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#delete(java.lang.Class,
	 * java.io.Serializable)
	 */
	public <M> void delete(Class<M> clazz, Serializable id) {
		this.getHibernateTemplate().delete(load(clazz, id));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#updateByQuery(java.lang.String,
	 * java.lang.Object[])
	 */
	public void updateByQuery(final String queryString,
			final Object[] parameters) {
		this.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString);
				if (parameters != null) {
					for (int i = 0; i < parameters.length; i++) {
						query.setParameter(i, parameters[i]);
					}
				}
				query.executeUpdate();
				return null;
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#load(java.lang.Class,
	 * java.io.Serializable)
	 */
	@SuppressWarnings("unchecked")
	public <M> M load(Class<M> entityClass, Serializable id) {
		return (M) this.getHibernateTemplate().load(entityClass, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#get(java.lang.Class,
	 * java.io.Serializable)
	 */
	@SuppressWarnings("unchecked")
	public <M> M get(Class<M> entityClass, Serializable id) {
		return (M) this.getHibernateTemplate().get(entityClass, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#findAll(java.lang.Class)
	 */
	@SuppressWarnings("unchecked")
	public <M> List<M> findAll(Class<M> entityClass) {
		return this.getHibernateTemplate().find(
				" from " + entityClass.getName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.been.common.dao.PublicDAO#deleteAll(java.lang.Class)
	 */
	public <M> Integer deleteAll(final Class<M> entityClass) {
		return (Integer) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(" delete from "
								+ entityClass.getSimpleName());
						return new Integer(query.executeUpdate());
					}
				});
	}
}
